//
//  GXTextField.m
//  ycPRO
//
//  Created by fang.jiaxin on 16/3/2.
//
//

#import "GXTextField.h"

@implementation GXTextField

-(instancetype)init
{
    self = [super init];
    if(self)
    {
        [self addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        self.textColor = [UIColor clearColor];
        self.tintColor = [UIColor clearColor];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        self.textColor = [UIColor clearColor];
        self.tintColor = [UIColor clearColor];
    }
    return self;
}

-(void)setLengthOfText:(NSUInteger)lengthOfText
{
    _lengthOfText = lengthOfText;
    if(self.labelArray == nil)
        self.labelArray = [NSMutableArray array];
    
    [self.labelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *view = obj;
        [view removeFromSuperview];
    }];
    [self.labelArray removeAllObjects];
    
    double width = self.frame.size.width/lengthOfText;
    CGRect frame = CGRectMake(0, 0,width , self.frame.size.height);
    for (int i = 0 ; i<lengthOfText; i++)
    {
        UILabel *label = [[UILabel alloc] initWithFrame:frame];
        label.font = self.font;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.borderWidth = 1;
        label.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5].CGColor;
        label.layer.masksToBounds = YES;
        label.backgroundColor = [UIColor whiteColor];
        label.userInteractionEnabled = YES;
        [self addSubview:label];
        [self bringSubviewToFront:label];
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tapGes.numberOfTapsRequired = 1;
        tapGes.numberOfTouchesRequired = 1;
        [label addGestureRecognizer:tapGes];
        
        [self.labelArray addObject:label];
        frame.origin.x+=width;
    }
    
    
}

-(void)tapAction:(UITapGestureRecognizer *)ges
{
    [self becomeFirstResponder];
}

-(void)textFieldDidChange:(id) sender
{
    NSString *text = self.text;
    [self.labelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *label = obj;
        
        if(idx<text.length)
        {
            NSRange range;
            range.location = idx;
            range.length = 1;
            label.text = [text substringWithRange:range];
            
            if(self.secureTextEntry)
                label.text = @"*";
        }
        else
            label.text = @"";
    }];
    
    
    if(text.length>=self.lengthOfText)
    {
        [self resignFirstResponder];
        
        
        if(text.length>self.lengthOfText)
        {
            NSRange range;
            range.location = 0;
            range.length = self.lengthOfText;
            self.text = [self.text substringWithRange:range];
        }
    }
}


-(void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    self.lengthOfText = self.lengthOfText;
}
@end
